/*
graceUI rich-text 加强工具
改造：微信风格缩放 + 自动判断图片是否需要缩放 H5
*/
var graceRichTextReg;

var GRT = [
	['h1', "font-size:3em; line-height:1.5em;"],
	['h2', "font-size:2em; line-height:1.8em;"],
	['h3', "font-size:1.6em; line-height:2em;"],
	['h4', "font-size:1.2em; line-height:2em;"],
	['h5', "font-size:1em; line-height:2em;"],
	['h6', "font-size:0.9em; line-height:2em;"],
	['p', "font-size:1em; width: auto;"],
	['b', "font-size:1em; line-height:2em;"],
	['strong', "font-size:1em; line-height:2em;"],
	['code', "font-size:1em; line-height:1.2em; background:#F6F7F8; padding:8px 2%; width:96%;"],
	['img', "display: block; margin: 8px auto; border-radius: 4px; object-fit: contain;"],
	['blockquote', "font-size:1em; border-left:3px solid #D1D1D1; line-height:2em; border-radius:5px; background:#F6F7F8; padding:8px 2%;"],
	['ul', "padding:5px 0; list-style:none; padding:0; margin:0;"],
	['li', "line-height:1.5em; padding:5px 0; list-style:none; padding:0; margin:0; margin-top:10px;"],
	['table', "width:100%; border-left:1px solid #F2F3F4; border-top:1px solid #F2F3F4;"],
	['th', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4;"],
	['td', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4; padding-left:5px;"]
];

export default {
	format: function(html){
		// 处理 <pre>
		html = html.replace(/<pre.*pre>?/gis, function(word){
			word =  word.replace(/[\n]/gi,'<br />');
			word =  word.replace(/    /gi,'<span style="padding-left:2em;"></span>');
			return word.replace(/[\t]/gi, '<span style="padding-left:2em;"></span>');
		});
		html = html.replace(/<pre/gi, '<p style="font-size:1em; margin:12px 0; line-height:1.2em; background:#F6F7F8; border-radius:5px; padding:8px 4%; width:92%;"');
		html = html.replace(/<\/pre/gi,"</p");

		// 样式替换
		for(let i = 0; i < GRT.length; i++){
			graceRichTextReg = new RegExp('<'+GRT[i][0]+'>|<'+GRT[i][0]+' (.*?)>', 'gi');
			html = html.replace(graceRichTextReg , function(word){
				if(word.indexOf('style=') != -1){
					var regIn = new RegExp('<' + GRT[i][0] + '(.*?)style="(.*?)"(.*?)(/?)>', 'gi');
					return word.replace(regIn, '<'+ GRT[i][0] +'$1style="$2 ' + GRT[i][1] +'"$3$4>');
				}else{
					var regIn = new RegExp('<' + GRT[i][0] + '(.*?)(/?)>', 'gi');
					return word.replace(regIn, '<'+ GRT[i][0] +'$1 style="' + GRT[i][1] +'$2">');
				}
			});
		}

		// 直接处理图片大小（去掉容器依赖）
		// #ifdef H5
		setTimeout(() => {
			if(document.querySelectorAll('.chat-img-150').length > 0) {
				document.querySelectorAll('.chat-img-150').forEach(img => {
					if (img.complete) {
						resizeImage(img);
					} else {
						img.onload = () => resizeImage(img);
					}
				});
			}
		}, 15);
		// #endif

		return html;

		function resizeImage(img) {
			const maxChatWidth = window.innerWidth * 0.5;   // 聊天气泡最大宽度
			const maxChatHeight = window.innerHeight * 0.5; // 聊天气泡最大高度
			const thumbWidth = maxChatWidth * 0.4;          // 缩略图宽度（长图用）
			const thumbHeight = maxChatHeight * 0.4;        // 缩略图高度（超宽图用）

			const naturalW = img.naturalWidth;
			const naturalH = img.naturalHeight;

			// ---- 情况 1：图片本身尺寸不大，直接原图 ----
			if (naturalW <= maxChatWidth && naturalH <= maxChatHeight) {
				img.style.width = naturalW + 'px';
				img.style.height = 'auto';
				return;
			}
		
			// ---- 情况 2：竖向长图（高度远大于宽度） ----
			if (naturalH / naturalW > 2) {
				img.style.width = thumbWidth + 'px'; // 长图缩成一半气泡宽度
				img.style.height = 'auto';
				return;
			}
		
			// ---- 情况 3：横向超宽图（宽度远大于高度） ----
			if (naturalW / naturalH > 2) {
				// 限制高度到缩略高度，宽度按比例缩放
				const scale = thumbHeight / naturalH;
				img.style.width = (naturalW * scale) + 'px';
				img.style.height = 'auto';
				
				return;
			}
		
			// ---- 情况 4：普通大图（宽或高超限） ----
			if (naturalW > maxChatWidth) {
				img.style.maxWidth = maxChatWidth*0.5 + 'px';
				img.style.height = 'auto';
				return
			}
			
			if (naturalH > maxChatHeight) {
				const scale = maxChatHeight / naturalH;
				img.style.width = (naturalW * scale)*0.7 + 'px';
				img.style.height = maxChatHeight + 'px';
				return
			}
			
		}
	}
};
